package Book.Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.BookDAO;
import booklist.Book;
import booklist.Page;

/**
 * Servlet implementation class ShowBook
 */
@WebServlet("/ShowBook")
public class ShowBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		BookDAO bookDao =new BookDAO();
		int curPage = 1;
		int totalCount = bookDao.getTotalCount();
		int totalPages=0;
		if(totalCount%5==0)
		{
			totalPages=totalCount/5;
		}
		else {
			totalPages=(totalCount/5)+1;
		}
		if(request.getParameter("curPage") != null){
			curPage = Integer.parseInt(request.getParameter("curPage"));
		}
		Page page = new Page(curPage, totalCount);
		page.setTotalPages(totalPages);
		List<Book> books = bookDao.getBookByPage(page);
		request.setAttribute("Books", books);
		request.setAttribute("page", page);
		System.out.println(totalCount);
		System.out.println(totalPages);
		request.getRequestDispatcher("showbook.jsp").forward(request, response);
        
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
